//
//  ViewController.m
//  myhuibao
//
//  Created by lijun on 13-2-4.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "MemberViewController.h"
#import "LoginViewController.h"
#import "Config.h"
#import "SBJson.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "MemberSettingsController.h"
#import "MemberFavoriteLisViewController.h"
#import "HomeViewController.h"
@interface MemberViewController ()

@end

@implementation MemberViewController
@synthesize lbTitle;
static int type;
UIAlertView *alert;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillAppear:(BOOL)animated
{
    [self hasLogin];
    
}
- (void)hasLogin
{
    int memberId = [[Config Instance] getMemberId];
    if(memberId == 0)
    {
        LoginViewController  *view = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        //    view.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }else
    {
        NSString * nickName = [[Config Instance] getNickName];
        if([nickName isEqualToString:@""]==NO)
            lbTitle.text = [NSString stringWithFormat:@"欢迎你，%@",nickName];
        else
            lbTitle.text = [NSString stringWithFormat:@"欢迎你，%@",[[Config Instance] getMobile]];
    }
}
- (void)viewDidUnload
{
    [self setLbTitle:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"我的惠宝";
    }
    return self;
}
- (void)dealloc {
    [lbTitle release];
    [super dealloc];
}
- (IBAction)accountSetting:(id)sender {
    MemberSettingsController  *view = [[MemberSettingsController alloc] initWithNibName:@"MemberSettingsController" bundle:nil];
    //    view.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:view animated:YES];
    [view release];
}

- (IBAction)loginOut:(id)sender {
    int memberid=[[Config Instance] getMemberId];
    NSString *stringInt = [NSString stringWithFormat:@"%d",memberid];
    NSURL *url=[NSURL URLWithString:url_loginout];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setPostValue: stringInt forKey:@"member.memberID"];
    [request setDelegate: self];
    [request startAsynchronous];
    LoginViewController *view=[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    [self.navigationController pushViewController:view animated:YES];
    [[Config Instance]setIsLogin:NO];
    [view release];
}

- (IBAction)favoriteList:(id)sender {
    MemberFavoriteLisViewController *view=[[MemberFavoriteLisViewController alloc]initWithNibName:@"MemberFavoriteLisViewController" bundle:nil];
    //view.hidesBottomBarWhenPushed=YES;
	[self.navigationController pushViewController:view animated:YES];
	[view release];
}

- (IBAction)pointBalance:(id)sender {
    type=1;
    int memberId=[[Config Instance]getMemberId];
    NSString *sid=[NSString stringWithFormat:@"%d",memberId];
    NSString *pwd=[[Config Instance]getPassword];
    NSURL *url=[NSURL URLWithString:url_pointBalance];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setPostValue:sid forKey:@"member.memberID"];
    [request setPostValue:pwd forKey:@"member.password"];
    [request setDelegate:self];
    [request startSynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    if(type==1){
        NSString *jsonStr = [request responseString];
        //NSLog(@"%@",jsonStr);
        NSDictionary *dic = [jsonStr JSONValue];
        
        NSDictionary *data= [dic valueForKey:@"data"];
        int point=[[data valueForKey:@"point"] intValue];
        double money=[[data valueForKey:@"money"]doubleValue];
        NSString *mesg=[NSString stringWithFormat:@"您的积分为:%d\n现金为:%9.2f",point,money];
        alert=[[UIAlertView alloc]initWithTitle:@"积分余额"
                                        message:mesg
                                       delegate:nil
                              cancelButtonTitle:nil
                              otherButtonTitles:nil];
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector: @selector(performDismiss:)
                                       userInfo:nil repeats:NO];
        [alert show];
    }
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
}
- (void) performDismiss: (NSTimer *)timer
{
    [alert dismissWithClickedButtonIndex:0 animated:NO];
    [alert release];
    alert = NULL;
}

@end
